<template>
  <FormView
    v-model="entity"
    :title="entity.id ? '修改区域管理' : '新建区域管理'"
    :toolbar-attrs="{ buttons: ['back', 'save', 'cancel'] }"
    :toolbar-listeners="{ save: persist }">
    <FormHolder ref="form" label-width="100px" :model="entity">
      <FormItemGroup block-title="基本信息">
        <FormTreeSelect
          v-model="entity.parentId"
          :span="12"
          label="父区域名称"
          :options="regionalTreeData"
          prop="parentId" />
        <FormInput v-model="entity.name" prop="name" label="区域名称" required />
        <FormInput v-model="entity.code" prop="code" label="区域编码" disabled />
        <FormInput v-model="entity.x" prop="x" label="经度" max-length="15" />
        <FormInput v-model="entity.y" prop="y" label="纬度" max-length="15" />
        <FormInputNumber v-model="entity.coverArea" prop="coverArea" label="占地面积" max-length="10" required />
        <FormInputNumber v-model="entity.floorage" prop="floorage" label="建筑面积" max-length="10" required />
        <FormInput v-model="entity.address" prop="address" label="区域地址" max-length="255" required />
        <FormInput v-model="entity.postalCode" prop="postalCode" label="邮政编号" max-length="20" />
        <FormInput v-model="entity.houseNumber" prop="houseNumber" label="门牌号" max-length="32" />
        <FormTreeSelect v-model="entity.landId" :span="12" label="所属土地" :options="landTreeData" prop="landId" />
        <FormTreeSelect
          v-model="entity.mgmtUnitId"
          :span="12"
          label="管理单位"
          :options="deptTreeList"
          prop="mgmtUnitId"
          required />
        <FormTextarea v-model="entity.remarks" rows="5" prop="remarks" label="备注" max-length="3000" />
      </FormItemGroup>
      <FormItemGroup :gutter="0" block-title="附件信息">
        <UploadPage
          v-if="entity.unid"
          ref="UploadPage"
          style="padding: 0"
          :unique-code="entity.unid"
          model="space"
          classify="regional" />
      </FormItemGroup>
    </FormHolder>
  </FormView>
</template>

<script>
  import { addObj, putObj, getObj, regionalFetchTree, landFetchTree, create } from '@/api/space/fyroomregional'
  import { fetchTree } from '@/api/admin/dept'
  import form from '@/vue/mixin/form'

  export default {
    name: 'RoomLandForm',
    relativeViewId: 'RegionalView',

    mixins: [form],
    data() {
      return {
        entity: {
          id: '',
          deptId: null,
          deptFullname: null,
          createBy: null,
          createId: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          unid: '',
          assetNumber: null,
          parentName: null,
          parentCode: null,
          parentId: null,
          name: null,
          code: null,
          fullcode: null,
          fullname: null,
          coverArea: null,
          floorage: null,
          address: null,
          postalCode: null,
          mgmtUnitId: null,
          mgmtUnitName: null,
          houseNumber: null,
          remarks: null,
          dataCode: null,
          ordernumber: null,
          x: null,
          y: null,
          landId: null,
          landName: null,
          level: null
        },
        deptTreeList: [],
        regionalTreeData: [],
        landTreeData: []
      }
    },
    created() {
      this.init()
    },
    methods: {
      create,
      load: getObj,
      save: addObj,
      update: putObj,
      async init() {
        const res = await fetchTree()
        this.deptTreeList = res.data.data

        const regionalRes = await regionalFetchTree()
        this.regionalTreeData = regionalRes.data.data

        const landRes = await landFetchTree()
        this.landTreeData = landRes.data.data
      }
    }
  }
</script>
